<template>
	<view class="haibao">
		<view class="haibao-top" v-if="show==false">
			<view class="haibao-img">
				<image :src="info.thumb"></image>
			</view>
			<view class="haibao-title">
				{{info.title || ''}}
			</view>
			<view class="goods-subtitle">
				<text>{{info.priceMarket || '0'}}</text>
			</view>

			<view class="info">
				<view class="info-left">
					<view class="goods-ewm">
						<image :src="code_img"></image>
					</view>
					<view class="goods-text">
						<view class="">
							① 保存图片到相册
						</view>
						<view class="">
							② 微信扫码可见
						</view>
					</view>
				</view>
				<view class="info-right">
					<image :src="vuex_config.comLogo"></image>
				</view>
			</view>
		</view>


		<view class="page-foot" v-if="show==false">
			<view class="safe-fixed-bottom">
				<view class="btn flex-center" :disabled="false" @click="doSaveImageToLocal()">
					保存到相册
				</view>
			</view>
		</view>



		<view class=" d-cn d-ac d-jc" v-if="show==true &&  path">
			<image :src="path" mode="widthFix" style="width:750rpx;"></image>
		</view>


		<l-painter v-if="show==true" path-type="url" isCanvasToTempFilePath @success="painter_success" hidden class="haibao"
			css="width:702rpx;background: #FFFFFF;border-radius: 16rpx;padding: 24rpx 24rpx 100rpx;">
			<l-painter-view>
				<l-painter-image v-if="info.thumb" :src="info.thumb" css="width:654rpx;height: 654rpx;" />
				<l-painter-text :text="info.title"
					css="margin-top:18rpx;font-size: 28rpx;font-weight: normal;color: #000000;" />
			</l-painter-view>
			<l-painter-view css="display: flex;align-items: center;margin-top:10rpx;">
				<l-painter-text text="￥" css="font-size: 20rpx;color: #000000;" />
				<l-painter-text :text="info.priceMarket" css="font-weight: normal;font-size: 32rpx;color: #000000;" />
			</l-painter-view>
			<l-painter-view css="margin-top: 16rpx;display: flex;justify-content: space-between;align-items: center;">
				<l-painter-view css="display: flex;align-items: center;">
					<l-painter-image v-if="code_img" :src="code_img" css="width: 104rpx;height: 104rpx;margin-right:20rpx;" />
					<l-painter-view css="display: flex;align-items: center;flex-direction: column;">
						<l-painter-view css="display: flex;align-items: center;">
							<l-painter-text text="① 保存图片到相册" css="font-size: 24rpx;color: #333333;" />
						</l-painter-view>
						<l-painter-view css="display: flex;align-items: center;">
							<l-painter-text text="② 微信扫码可见" css="font-size: 24rpx;color: #333333;" />
						</l-painter-view>
					</l-painter-view>
				</l-painter-view>
				<l-painter-view css="display: flex;align-items: center;width: 100rpx;height: 100rpx;">
					<l-painter-image :src="vuex_config.comLogo" css="width: 100rpx;height: 100rpx;" />
				</l-painter-view>
			</l-painter-view>
		</l-painter>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				inventoryId: '',
				path: '',
				show: false,
				info: {
					images: []
				},
				code_img: ''
			};
		},

		// onShareAppMessage(res) {
		// 	let userId = uni.getStorageSync('userId') || '';
		// 	console.log(res, '1000')
		// 	if (res.from === 'button') { // 来自页面内分享按钮
		// 		return {
		// 			title: this.info.title || '',
		// 			path: '/pages/product/product-detail?invite_id=' + userId + '&id=' + this.info.inventoryId
		// 		}
		// 	} else {
		// 		return {
		// 			title: this.info.title,
		// 			imageUrl: this.info.images[0],
		// 			path: '/pages/product/product-detail?invite_id=' + userId + '&id=' + this.info.inventoryId
		// 		}
		// 	}
		// },
		// onShareTimeline() {
		// 	let userId = uni.getStorageSync('userId') || '';

		// 	return {
		// 		title: this.info.title,
		// 		imageUrl: this.info.images[0],
		// 		query: 'invite_id=' + userId + '&id=' + this.info.inventoryId,
		// 		path: '/pages/product/product-detail'
		// 	}
		// },


		onLoad(option) {
			this.inventoryId = option.id || ''
			this.query_product_info()
			this.query_product_code()
		},
		methods: {
			//产品信息
			query_product_info() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'product_detail',
						inventoryId: this.inventoryId
					},
				}).then(res => {
					if (res.code == 200) {
						this.info = res.data
					}
				})
			},
			// 产品二维码
			query_product_code() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'product_getCode',
						inventoryId: this.inventoryId
					},
				}).then(res => {
					if (res.code == 200) {
						this.code_img = res.data.code
					}
				})
			},

			//海报图片保存到本地
			doSaveImageToLocal() {
				this.show = true
				uni.showLoading({
					title: '生成中...'
				})
				let ptime = setInterval(() => {
					if (this.path) {
						uni.hideLoading()
						clearInterval(ptime)
						this.doSaveToLocal()
					}
				}, 500)
			},

			doSaveToLocal(data) {
				console.log(this.path, '临时图片')
				uni.hideLoading()
				uni.saveImageToPhotosAlbum({
					filePath: this.path,
					success: () => {
						this.$alert('图片保存成功')
					},
					fail: (e) => {
						console.log(e)
					}
				});
			},

			painter_success(e) {
				// this.path = $event
				this.path = e;
				console.log('this.path', this.path)
			},

		}
	}
</script>

<style lang="less">
	page {
		background: #F8F8F8;
		background: #F6FAFD;
	}
</style>
<style lang="scss" scoped>
	.page-foot {
		.btn {
			width: 100%;

			height: 80rpx;
			background: #F8F8F8;
			border-radius: 40rpx 40rpx 40rpx 40rpx;
			font-family: PingFang SC, PingFang SC;

			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #545B6A;
		}
	}


	.haibao {
		.haibao-top {
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 24rpx 24rpx 10rpx;
			margin: 26rpx 24rpx;

			.haibao-img {
				width: 654rpx;
				height: 654rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.haibao-title {
				margin-top: 40rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: normal;
				font-size: 28rpx;
				color: #000000;
			}

			.goods-subtitle {
				margin-top: 10rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				text {
					&:nth-child(1) {
						// font-weight: normal;
						font-size: 32rpx;
						color: #000000;

						&:before {
							content: "￥";
							font-size: 20rpx;
							color: #000000;
						}
					}

					&:nth-child(2) {
						font-size: 32rpx;
						// font-weight: normal;
						color: #000000;
					}
				}
			}

			.info {
				margin-top: 10rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.info-left {
					display: flex;
					align-items: center;

					.goods-ewm {
						image {
							width: 104rpx;
							height: 104rpx;
							margin-right: 20rpx;
						}
					}

					text {
						font-size: 26rpx;
						color: #333333;
					}

					.goods-text {
						font-size: 24rpx;
						color: #333333;

						view {
							display: flex;
							align-items: center;
							margin-bottom: 16rpx;

							&:nth-last-of-type(1) {
								margin-bottom: 0;
							}
						}
					}
				}

				.info-right {
					image {
						width: 101rpx;
						height: 101rpx;
					}
				}
			}
		}

		.poster-bot {
			padding: 28rpx 30rpx 100rpx;
			background: #FFFFFF;
			border-radius: 40rpx 40rpx 0rpx 0rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.share-box {
				display: flex;
				flex-direction: column;
				justify-content: center;

				button {
					display: flex;
					flex-direction: column;
					background: transparent;
				}

				image {
					width: 92rpx;
					height: 92rpx;
					margin-bottom: 26rpx;
				}

				text {
					font-size: 24rpx;
					color: #333333;
					line-height: 26rpx;
				}
			}

			.btn {
				height: 100rpx;
				line-height: 100rpx;
				background: #E96600;
				border-radius: 50rpx;
				text-align: center;
				margin-top: 62rpx;

				text {
					font-size: 34rpx;
					color: #FFFFFF;
				}
			}
		}
	}
</style>